<?php
            


/**
 * Author: cbw
 * Desc: 代言模式
 * Time: 2022/1/6 2:30 下午
 * Email: 17854288794@163.com
 */

require_once PATH_MODULE .'define.php';
class DaiYanModel extends PluginModel {

    /**
     * Desc:获取用户代言列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 6:03 下午
     * @param $themeid
     * @param int $daiyan_status
     * @param string $sort
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getDaiYanList($themeid,$daiyan_status=0,$sort='',$page_start=0,$page_size=10)
    {
        $condition = "";
        if ($daiyan_status){
            $condition .= " and daiyan_status='".$daiyan_status."'";
        }
        if ($sort=='daiyan_liked'){
            $condition .= " order by daiyan_liked desc ";
        }elseif ($sort=='addtime'){
            $condition .= " order by addtime desc ";
        }
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " select * from ".tablename(t_record)." where themeid=$themeid  and address!='' ".$condition;
        return db_select($sql);
    }

    /**
     * Desc:搜索代言
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 6:43 下午
     * @param $themeid
     * @param $search
     * @param int $page_start
     * @param int $page_size
     * @param $start_day
     * @param $end_day
     * @return mixed
     */
    public static function searchDaiYanList($themeid,$search,$page_start=0,$page_size=10,$start_day='',$end_day='')
    {
        $condition="";
        if ($search){
            $search = '%'.$search.'%';
            $condition .= " AND nickname LIKE '".$search."'"." or mobile LIKE '".$search."' ".
                " or username LIKE '".$search."' ";
        }

        if ($start_day && $end_day){
            $condition .= "  and addtime between '".$start_day."' and '".$end_day."'";
        }
        $condition .= " ORDER BY addtime DESC ";
        if ($page_start) {
            $condition.= "LIMIT ".$page_start.",".$page_size;
        }
        $sql = " select * from ".tablename(t_record)." where themeid=$themeid  and address!='' ".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取查询条件下的代言的数量
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 6:07 下午
     * @param $where
     * @return int
     */
    public static function getDaiYanCountByWhere($where)
    {
        $sql = " select count(*) as cnt from ".tablename(t_record).$where;
        return intval(db_find($sql)['cnt']);
    }

    /**
     * Desc:获取代言状态的数量
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 6:24 下午
     * @param $themeid
     * @return array[]
     */
    public static function getDaiYanStatusArr($themeid)
    {
        $temp_arr = [
            ['title'=>'全部','status'=>0,'total'=>0],
            ['title'=>'待审核','status'=>1,'total'=>0],
            ['title'=>'审核通过','status'=>2,'total'=>0],
            ['title'=>'审核未通过','status'=>3,'total'=>0],
        ];
        foreach ($temp_arr as $k => $val){
            $temp_where = " where themeid=".$themeid."  and address!='' ";
            if ($val['status']){
                $temp_where.=' and daiyan_status='.$val['status'];
            }
            $temp_arr[$k]['total'] = self::getDaiYanCountByWhere($temp_where);
        }
        return $temp_arr;
    }

    /**
     * Desc:修改代言评论
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/21 11:23 上午
     * @param $where
     * @param $data
     * @return bool
     */
    public static function editDaiYanComment($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s",time());
        $res = db_update(t_daiyan_user_refund,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Desc:修改用户发表的代言item
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 6:27 下午
     * @param $where
     * @param $data
     * @return bool
     */
    public static function editDaiYanItem($where,$data)
    {
        $res = db_update(t_record,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Desc:通过查询条件获取代言item
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 6:29 下午
     * @param $where
     * @return mixed
     */
    public static function getDaiYanItemByWhere($where)
    {
        $res = db_get(t_record,$where);
        return $res;
    }

    /**
     * Desc:插入代言回复
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/6 7:08 下午
     * @param $data
     * @return false
     */
    public static function insertDaiYanRefund($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s",time());
        $res = $insertid = db_insert(t_daiyan_user_refund,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    /**
     * Desc:获取前台代言数据 当前用户不处理状态 其他用户处理状态
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/10 10:25 上午
     * @param $themeid
     * @param int $uid
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getFrontDaiYanList($themeid,$uid=0,$page_start=0,$page_size=10)
    {
        $select_ids = " ";
        $pass_list = self::getDaiYanListIdByWhere($themeid,0,2);
        if ($pass_list){
            foreach ($pass_list as  $v){
                $select_ids .= $v['id'].",";
            }
        }
        $user_list = self::getDaiYanListIdByWhere($themeid,$uid,0);
        if ($user_list){
            foreach ($user_list as  $v){
                $select_ids .= $v['id'].",";
            }
        }
        $select_ids = " ( ".rtrim($select_ids,',')." ) ";

        $condition = " and id in ".$select_ids;
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " select id,avatar,uid,themeid,nickname,username,mobile,datetime,address,daiyan_liked,daiyan_status,daiyan_worker_id from ".tablename(t_record)
        ." where themeid=$themeid  and address!='' ".$condition;
        return db_select($sql);
    }

    /**
     * Desc:通过查询条件获取对应的代言列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/16 8:06 上午
     * @param $themeid
     * @param int $uid
     * @param int $daiyan_status
     * @return mixed
     */
    public static function getDaiYanListIdByWhere($themeid,$uid=0,$daiyan_status=2)
    {
        $condition = "";
        if ($uid){
            $condition .= " and uid=$uid";
        }
        if ($daiyan_status){
            $condition .= " and daiyan_status=$daiyan_status";
        }
        $sql = " select id from ".tablename(t_record)." where themeid=$themeid  ".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取代言下的回复数量
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/10 10:30 上午
     * @param $themeid
     * @param $return_uid
     * @return mixed
     */
    public static function getDaiYanRefundTotal($themeid,$return_uid)
    {
        $sql = " select count(*) as cnt from ".tablename(t_daiyan_user_refund)." where return_uid=$return_uid and themeid=$themeid and status=2";
        return intval(db_find($sql)['cnt']);
    }

    /**
     * Desc:评论回复列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 8:26 下午
     * @param $themeid
     * @param $return_uid
     * @param string $search
     * @param int $page_start
     * @param int $page_size
     * @param string $start_day
     * @param string $end_day
     * @return mixed
     */
    public static function getDaiYanReturnListOrSearch($themeid,$return_uid,$search='',$page_start=0,$page_size=10,$start_day='',$end_day='')
    {
        $condition = "";
        if ($search){
            $search = '%'.$search.'%';
            $condition .= " AND b.nickname LIKE '".$search."'"." or b.mobile LIKE '".$search."' ".
                " or b.username LIKE '".$search."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."'";
        }
        $condition .= " order by a.created_at desc ";
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " select a.*,b.avatar,b.nickname,b.mobile from ".tablename(t_daiyan_user_refund)." as a ".
            " left join ".tablename(t_record)." as b on a.uid=b.uid and b.themeid=$themeid".
            " where a.themeid=$themeid and a.return_uid=$return_uid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取回复的记录 当前用户状态不判断  其他用户通过的状态
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 10:18 上午
     * @param $return_uid
     * @param $themeid
     * @return mixed
     */
    public static function getDaiYanReturnList($return_uid,$themeid,$uid)
    {
        $select_ids = "";
        $pass_list = self::getDaiYanReturnListByWhere(['themeid'=>$themeid,'status'=>2]);
        if ($pass_list){
            foreach ($pass_list as  $v){
                $select_ids .= $v['id'].",";
            }
        }
        $user_list = self::getDaiYanReturnListByWhere(['themeid'=>$themeid,'uid'=>$uid]);
        if ($user_list){
            foreach ($user_list as  $v){
                $select_ids .= $v['id'].",";
            }
        }
        $condition = " ";
        $select_ids = rtrim($select_ids,',');

        if ($select_ids){
            $select_ids = " ( ".rtrim($select_ids,',')." ) ";
            $condition = " and a.id in ".$select_ids;
        }

        $sql = " select a.*,b.avatar,b.nickname from ".tablename(t_daiyan_user_refund)." as a ".
        " left join ".tablename(t_record)." as b on a.uid=b.uid and b.themeid=$themeid".
        " where a.themeid=$themeid and a.return_uid=$return_uid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:根据条件获取代言评论
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 4:08 下午
     * @param $where
     * @return mixed
     */
    public static function getDaiYanReturnListByWhere($where)
    {
        return db_getall(t_daiyan_user_refund,$where);
    }

    /**
     * Desc:获取前端展示的代言列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 9:14 上午
     * @param $themeid
     * @param int $uid
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function formatFrontDaiYan($themeid,$uid=0,$page_start=0,$page_size=10)
    {
        $daiyan_list = self::getFrontDaiYanList($themeid,$uid,$page_start,$page_size);
        foreach ($daiyan_list as $key => $val){
            if (!$val['datetime']){
                $daiyan_list[$key]['datetime'] = 1;
            }
            $return_total = self::getDaiYanRefundTotal($themeid,$val['uid']);
            $daiyan_list[$key]['hidden_txt']=true;
            $daiyan_list[$key]['return_total']=$return_total;
            $daiyan_list[$key]['return_hidden']=true;
            $return_list = self::getDaiYanReturnList($val['uid'],$themeid,$uid);
            foreach ($return_list as $k => $v){
                $return_list[$k]['sort'] = $k+1;
            }
            $daiyan_list[$key]['return_list'] = $return_list;
        }
        return $daiyan_list;
    }

    /**
     * Desc:好友助力点赞
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 8:50 上午
     * @param $themeid
     * @param $uid
     * @param $help_uid
     */
    public static function userLiked($themeid,$uid,$help_uid,$id)
    {
        $has_help = self::getFriendHasFlag($themeid,$uid,$help_uid);
        if (!$has_help){
            $data = [
                'uid'=>$help_uid,
                'themeid'=>$themeid,
                'help_uid'=>$uid
            ];
            $res = self::insertFriendHelp($data);
            if ($res){
                $sql = " update ".tablename(t_record)." set daiyan_liked=daiyan_liked+1 where id=$id  and uid=$help_uid and themeid=$themeid";
                db_find($sql);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * Desc:好友评论代言
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 9:16 上午
     * @param $themeid
     * @param $uid
     * @param $return_uid
     */
    public static function addUserReturn($themeid,$uid,$return_uid,$content)
    {
        $insert_data = [
            'uid'=>$uid,
            'themeid'=>$themeid,
            'content'=>$content,
            'return_uid'=>$return_uid
        ];
        $res = self::insertUserComment($insert_data);
        if ($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * Desc:插入代言评论
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 3:55 下午
     * @param $data
     * @return false
     */
    public static function insertUserComment($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s",time());
        $res = $insertid = db_insert(t_daiyan_user_refund,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    /**
     * Desc:获取好友是否有助力记录
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/14 9:55 下午
     * @param $themeid
     * @param $uid
     * @param $help_uid
     * @return mixed
     */
    public static function getFriendHasFlag($themeid,$uid,$help_uid)
    {
        $sql = " SELECT * FROM ".tablename(t_daiyan_user_help)." where themeid=$themeid and uid=$help_uid and help_uid=$uid";
        return db_find($sql);
    }

    /**
     * Desc:插入好友助力
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 8:48 上午
     * @param $data
     * @return false
     */
    public static function insertFriendHelp($data)
    {
//        $data['updated_at'] = date("Y-m-d H:i:s",time());
        $res = $insertid = db_insert(t_daiyan_user_help,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    /**
     * Desc:获取好友助力记录
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/17 4:36 下午
     * @param $themeid
     * @param $uid
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getFriendHelpFlagByWhere($themeid,$uid,$page_start=0,$page_size=10,$start_day='',$end_day='',$search='')
    {
        $condition = "";
        if ($search){
            $search = '%'.$search.'%';
            $condition .= " AND b.nickname LIKE '".$search."'"." or b.mobile LIKE '".$search."' ".
                " or b.username LIKE '".$search."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."'";
        }
        $condition .= " order by a.created_at desc ";
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " select a.uid,a.help_uid,a.created_at,b.username,b.nickname,b.avatar from ".tablename(t_daiyan_user_help).
            " as a  ".
            " left join ".tablename(t_record)." as b on b.uid=a.help_uid and b.themeid=$themeid".
            " where a.themeid=$themeid and a.uid=$uid  ".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取助力记录数
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 5:12 下午
     * @param $themeid
     * @param $uid
     * @return mixed
     */
    public static function getFriendHelpFlagTotal($themeid,$uid)
    {
        $sql = " select count(*) as cnt from ".tablename(t_daiyan_user_help)." where themeid=$themeid and uid=$uid";
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取用户带言值排序
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/14 10:08 下午
     * @param $themeid
     * @param $uid
     * @return mixed
     */
    public static function getUserDaiYanRankList($themeid,$page_start=0,$page_size=10,$page_index)
    {
        $condition = '';
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " select nickname,avatar,username,daiyan_liked,address,datetime from ".tablename(t_record).
            " where themeid=$themeid  and  daiyan_status=2 order by daiyan_liked desc,addtime asc ".$condition;
        $list =  db_select($sql);
        foreach ($list as $k => $val){
            $list[$k]['sort'] = ($page_size*($page_index-1))+($k+1);
        }
        return $list;
    }

    /**
     * Desc:获取当前用户的代言排名信息
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/21 3:12 下午
     * @param $themeid
     * @param $uid
     */
    public static function getUserDaiYanSort($themeid,$uid)
    {
        $error = '暂无排名';
        $sort = 0;
        if ($uid){
            $sql = " select uid,nickname,avatar,username,daiyan_liked,address,datetime from ".tablename(t_record).
                " where themeid=$themeid  and  daiyan_status=2 order by daiyan_liked desc,addtime asc ";
            $list =  db_select($sql);
            foreach ($list as $k => $v){
                if ($v['uid']==$uid){
                    $sort = ($k+1);
                    $error = '';
                }
            }
        }

        return ['error'=>$error,'sort'=>$sort];
    }

    /**
     * Desc:获取排序代言下的总数量
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/18 4:55 下午
     * @param $themeid
     * @return mixed
     */
    public static function getUserDaiYanRankTotal($themeid)
    {
        $sql = " select count(*) as cnt from ".tablename(t_record).
            " where themeid=$themeid  and  daiyan_status=2 order by daiyan_liked desc";
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取查询条件下的订单item
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2022/1/19 3:51 下午
     * @param $where
     * @return mixed
     */
    public static function getUserRecordItem($where)
    {
        return db_get(t_record,$where);
    }
}